package com.cpecc.dao.system;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.BiZhong;

@Repository
public class BiZhongDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(BiZhong biZhong){
		Session session=sessionFactory.getCurrentSession();
		session.save(biZhong);
		session.flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<BiZhong> getAllBiZhong(){
		String hql="from BiZhong";
		return sessionFactory.getCurrentSession().createQuery(hql).list();
	}
	
	public boolean cover(BiZhong biZhong){
		String hql="update BiZhong t set t.no=?,t.biZhongMC=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, biZhong.getNo());
		query.setString(1, biZhong.getBiZhongMC());
		query.setString(2, biZhong.getId());
		return (query.executeUpdate()>0);
	}
	
	public void delete(BiZhong biZhong){
		Session session=sessionFactory.getCurrentSession();
		session.delete(biZhong);
		session.flush();
	}
	
	public BiZhong get(String id){
		return (BiZhong) sessionFactory.getCurrentSession().get(BiZhong.class, id);
	}
}
